﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;
using Acms.DataControls;

public partial class Templates_NormalSection_Default : BaseTemplatePage
{
    protected override void AttachEvents()
    {
        acmsQuote.DataBound += new EventHandler(acmsQuote_DataBound);
        acmsQuoteMaker.DataBound += new EventHandler(acmsQuoteMaker_DataBound);
        ucCasesFeedModule.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Post>>(ucCasesFeedModule_SelectData);
        ucExploreModule.DataRequired += new EventHandler(ucExploreModule_DataRequired);
        ucNewsFeedModule.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Post>>(ucNewsFeedModule_SelectData);
        ucTestamonialsModule.DataRequired += new EventHandler(ucTestamonialsModule_DataRequired);
        ucKeyPeople.SelectData += new EventHandler<SelectEventArgs<Post>>(ucKeyPeople_SelectData);
        acmsDownload1.DataBound += new EventHandler(acmsDownload1_DataBound);
        acmsDownload2.DataBound += new EventHandler(acmsDownload2_DataBound);
    }


    void acmsDownload1_DataBound(object sender, EventArgs e)
    {
        if (!acmsDownload1.IsEmpty)
        {
            phDownloads.Visible = true;
            liDownload1.Visible = true;
            acmsDownload1.CssClass = string.Format("ico_{0}", acmsDownload1.Extension);
        }
    }

    void acmsDownload2_DataBound(object sender, EventArgs e)
    {
        if (!acmsDownload2.IsEmpty)
        {
            phDownloads.Visible = true;
            liDownload2.Visible = true; 
            acmsDownload2.CssClass = string.Format("ico_{0}", acmsDownload2.Extension);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string[] paths = null;
            if (AcmsContext.CurrentPost != null)
            {
                //Response.Write(AcmsContext.CurrentPost.Path);
                paths = AcmsContext.CurrentPost.Path.Split('/');
                //System.Text.StringBuilder tmp = new System.Text.StringBuilder();
                //string spacer = "";
                //foreach (string t in paths)
                //{
                //    tmp.Append(spacer);
                //    tmp.Append(t);
                //    spacer = " _ ";
                //}
                //Response.Write(tmp.ToString());
                if (paths.Length >= 3)
                {
                    SectionClass = string.Concat(paths[1].ToLower(), paths[2]).Replace(" ", "");
                    SectionTopic = Topic.GetBySiteIdTopicGroupPathAndName(AcmsContext.SiteId, paths[1], paths[2]);
                }
                else if (paths.Length == 2)
                {
                    SectionTopicGroup = TopicGroup.GetBySiteIdAndPath(AcmsContext.SiteId, paths[1]);
                    useTopic = false;
                }

                if (paths.Length <= 3)
                {
                    lblTitle.Text = AcmsContext.CurrentPost.DisplayName;
                    articleTitle.Visible = false;
                }
                else if (paths.Length == 4)
                {
                    lblTitle.Text = AcmsContext.CurrentPost.Parent.DisplayName;
                }
                else if (paths.Length > 2 && (paths[1].ToLower() == "services" || paths[1].ToLower() == "sectors"))
                {
                    Post root = Post.GetBySiteIdAndPath(AcmsContext.SiteId, String.Concat(paths[0], "/", paths[1], "/", paths[2]), AcmsContext.CurrentPost.SiteVersion);
                    if (root != null)
                    {
                        lblTitle.Text = root.DisplayName;
                    }
                }
                else if (paths.Length > 2)
                {
                    Post root = Post.GetBySiteIdAndPath(AcmsContext.SiteId, String.Concat(paths[0], "/", paths[1]), AcmsContext.CurrentPost.SiteVersion);
                    if (root != null)
                    {
                        lblTitle.Text = root.DisplayName;
                    }
                }
            }
            else
            {
                lblTitle.Text = "Section Title Here";
            }
            DataBind();
        }
    }

    void ucTestamonialsModule_DataRequired(object sender, EventArgs e)
    {
        List<Post> tmp = null;
        if (useTopic && SectionTopic != null)
        {
            tmp = Post.GetAllByTopicIdAndParentPath(AcmsContext.SiteId, SectionTopic.Id, "~/Testamonials", AcmsContext.SiteVersion);
        }
        else if (!useTopic && SectionTopicGroup != null)
        {
            tmp = Post.GetAllByTopicGroupIdAndParentPath(AcmsContext.SiteId, SectionTopicGroup.Id, "~/Testamonials", AcmsContext.SiteVersion);
        }
        ucTestamonialsModule.DataSource = tmp;
    }

    void ucExploreModule_DataRequired(object sender, EventArgs e)
    {
        if (AcmsContext.CurrentPost != null)
        {
            ucExploreModule.DataSource = AcmsContext.CurrentPost.Children;
        }
        else
        {
            ucExploreModule.DataSource = DummyPost.CreateDummyList(12);
        }
    }

    private List<Post> GetFeedData(string path)
    {
        if (AcmsContext.CurrentPost != null)
        {
            if (useTopic && SectionTopic != null)
            {
                return GetArticles(Post.GetAllByTopicIdAndParentPath(AcmsContext.SiteId, SectionTopic.Id, path, AcmsContext.SiteVersion));
            }
            else if (!useTopic && SectionTopicGroup != null)
            {
                return GetArticles(Post.GetAllByTopicGroupIdAndParentPath(AcmsContext.SiteId, SectionTopicGroup.Id, path, AcmsContext.SiteVersion));
            }
            else
            {
                return new List<Post>();
            }
        }
        else
        {
            return DummyPost.CreateDummyList(12);
        }
    }

    void ucNewsFeedModule_SelectData(object sender, Acms.DataControls.SelectEventArgs<Post> e)
    {
        e.SetData(GetFeedData("~/news"));
    }

    void ucCasesFeedModule_SelectData(object sender, Acms.DataControls.SelectEventArgs<Post> e)
    {
        e.SetData(GetFeedData("~/cases"));
    }

    protected string SectionClass {get; private set;}

    private int sectionTopicId;
    private Topic sectionTopic;
    protected Topic SectionTopic 
    {
        get 
        {
            if (sectionTopic == null && sectionTopicId > 0)
            {
                sectionTopic = Topic.GetById(sectionTopicId);
            }
            return sectionTopic;
        }
        set 
        {
            if (value != null)
            {
                sectionTopicId = value.Id;
            }
            sectionTopic = value;
        }
    }

    private int sectionTopicGroupId;
    private TopicGroup sectionTopicGroup;
    protected TopicGroup SectionTopicGroup
    {
        get
        {
            if (sectionTopicGroup == null && sectionTopicGroupId > 0)
            {
                sectionTopicGroup = TopicGroup.GetById(sectionTopicGroupId);
            }
            return sectionTopicGroup;
        }
        set
        {
            if (value != null)
            {
                sectionTopicGroupId = value.Id;
            }
            sectionTopicGroup = value;
        }
    }

    private bool useTopic = true;

    protected override object SaveViewState()
    {
        ViewState["UserTopic"] = useTopic;
        ViewState["SectionClass"] = SectionClass;
        ViewState["SectionTopicId"] = sectionTopicId;
        ViewState["SectionTopicGroupId"] = sectionTopicGroupId;
        return base.SaveViewState();
    }

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        if (ViewState["UserTopic"] is bool)
        {
            useTopic = (bool)ViewState["UserTopic"];
        }
        SectionClass = ViewState["SectionClass"] as string;
        if (ViewState["SectionTopicId"] is int)
        {
            sectionTopicId = (int)ViewState["SectionTopicId"];
        }
        if (ViewState["SectionTopicGroupId"] is int)
        {
            sectionTopicGroupId = (int)ViewState["SectionTopicGroupId"];
        }
    }

    void acmsQuoteMaker_DataBound(object sender, EventArgs e)
    {
        if (acmsQuoteMaker.IsEmpty)
        {
            quoteMakerWrapper.Visible = false;
        }
        else
        {
            quoteMakerWrapper.Visible = true;
        }
    }

    void acmsQuote_DataBound(object sender, EventArgs e)
    {
        if (acmsQuote.IsEmpty)
        {
            quoteWrapper.Visible = false;
        }
    }

    void ucKeyPeople_SelectData(object sender, SelectEventArgs<Post> e)
    {
        if (AcmsContext.CurrentPost != null)
        {
            //Response.Write(string.Format("User Topic = {0};", useTopic));
            List<Post> tmp = null;
            if (useTopic && SectionTopic != null)
            {
                //Response.Write(string.Format("Section = {0};", sectionTopic.Name));
                tmp = Post.GetAllByTopicIdAndParentPath(AcmsContext.SiteId, SectionTopic.Id, "~/KeyPeople/FeeEarners", AcmsContext.SiteVersion);
            }
            else
            {
                tmp = Post.GetAllBySiteIdAndParentPath(AcmsContext.SiteId, "~/KeyPeople/FeeEarners", AcmsContext.SiteVersion);
            }
            e.SetData(tmp);
        }
        else
        {
            e.SetData(DummyPost.CreateDummyList(6));
        }
    }


}
